﻿using UnityEngine;
using System.Collections.Generic;

namespace Battle
{
    public class FriendPVPBattleBalance : BalanceBase
    {
        public BalanceExpBar expBar;

        public UILabel attackerNameLabel;
        public UILabel defenderNameLabel;
        public UI.TextureKit attackerHeadIcon;
        public UI.TextureKit defenderHeadIcon;

        public UILabel loseAttackerNameLabel;
        public UILabel loseDefenderNameLabel;
        public UI.TextureKit loseAttackerHeadIcon;
        public UI.TextureKit loseDefenderHeadIcon;

        public UILabel tipLabel;

        protected override void Refresh()
        {
            int preLevel = StaticData.playerData.level;
            int preExp = (int)StaticData.playerData.exp;
            base.Refresh();

            ProtoBuf.Message.ResultObject resultObject = null;
            expBar.Init(preExp, preLevel, GetAwardThingNum(resultObject, Dict.Config.TableConst.exp), (int)StaticData.playerData.exp, StaticData.playerData.level);

            var dh0 = Dict.Blo.DictHeroBlo.GetTableHero(StaticData.curBattleData.attackFighters[0].actors[0].heroId);
            var dh1 = Dict.Blo.DictHeroBlo.GetTableHero(StaticData.curBattleData.defendFighters[0].actors[0].heroId);
            if (result == BalanceResult.Win)
            {
                expBar.Play();

                attackerHeadIcon.textureName = dh0.iconName;
                defenderHeadIcon.textureName = dh1.iconName;
                defenderHeadIcon.texture.material = AssetLoad.ResourceManager.Instance.LoadResource("shaders/heroIcon_circle_colorness") as Material;
                attackerNameLabel.text = StaticData.curBattleData.attackFighters[0].role.nickName;
                defenderNameLabel.text = "[2f2f2f]" + StaticData.curBattleData.defendFighters[0].role.nickName + "[-]";

                float tweenerTime = 0f;
                foreach (var tweener in tweeners)
                {
                    if (tweener != null)
                    {
                        tweener.ResetToBeginning();
                        tweener.PlayForward();
                        if (tweenerTime <= tweener.duration + tweener.delay)
                        {
                            tweenerTime = tweener.duration + tweener.delay;
                        }
                    }
                }
                //lastTweener.SetOnFinished(delegate {
                //    totalMask.SetActive(false);
                //    foreach (var drawBtn in drawButtons)
                //    {
                //        drawBtn.Holde();
                //    }
                //});

                mask.GetComponent<BoxCollider>().enabled = true;
                tipLabel.gameObject.SetActive(true);
            }
            else
            {
                expBar.Over();

                loseAttackerHeadIcon.textureName = dh0.iconName;
                loseDefenderHeadIcon.textureName = dh1.iconName;
                loseAttackerHeadIcon.texture.material = AssetLoad.ResourceManager.Instance.LoadResource("shaders/heroIcon_circle_colorness") as Material;
                loseAttackerHeadIcon.texture.material.shader = Shader.Find("Unlit/Masked DJMaskColorless");
                loseAttackerNameLabel.text = "[2f2f2f]" + StaticData.curBattleData.attackFighters[0].role.nickName + "[-]";
                loseDefenderNameLabel.text = StaticData.curBattleData.defendFighters[0].role.nickName;
                tipLabel.gameObject.SetActive(true);
            }
            totalMask.SetActive(false);
        }

        public override void AddBalanceReward()
        {

            if (GuideManager.Instance != null && GuideManager.Instance.GetRunningGuide().Equals("guide_pvp"))
            {
                var msg = GetResultMsg() as ProtoBuf.Message.SCPvpFightResultGuide;
                if (msg != null)
                {
                    StaticData.playerData.AddResultObj(msg.result);
                }
            }
            else
            {
                var msg = GetResultMsg() as ProtoBuf.Message.SCPvpFightResult;
                if (msg != null)
                {
                    StaticData.playerData.AddResultObj(msg.result);
                }
            }

        }

        public override void Exit()
        {
            base.Exit();
            Hide();
            ManagerController.Instance.BattleToRoomScene();
            Destroy(gameObject);
        }

        public override void OnTotalMaskClick()
        {
            base.OnTotalMaskClick();
            StopAllCoroutines();
            expBar.Over();
        }

    }
}
